package TEst1;

import java.util.Stack;

public class Test3 {
    public static boolean isValid(String s) {
        //初始化一个栈
        Stack<Character> stack=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch=s.charAt(i);
            //左括号进入这里
            if(ch=='{'||ch=='('||ch=='[') {
                stack.add(ch);
                //右括号进入这里
            } else {
                if(stack.empty()) {
                    return false;
                }//从栈中拿出来的一定是左括号
                char c=stack.pop();
                    if((c=='['&&ch!=']')||(c=='{'&&ch!='}')||(c=='('&&ch!=')')){
                    return false;
                }
            }
        }
        return true;
    }


    public int evalRPN(String[] tokens) {
    //逆波兰求值
        Stack<Integer> stack=new Stack<>();
        for (String x:tokens) {
            if(!x.equals("+")&&!x.equals("-")&&!x.equals("*")&&!x.equals("/")) {
              stack.add( Integer.parseInt(x));
            } else {
                int a1=stack.pop();
                int a2=stack.pop();
                switch (x) {
                    case "+":
                        stack.add(a2+a1);
                        break;
                    case "-":
                        stack.add(a2-a1);
                        break;
                    case "*":
                        stack.add(a2*a1);
                        break;
                    case "/":
                        stack.add(a2/a1);
                        break;
                }
            }
        }
        return stack.pop();

    }
    public static boolean IsPopOrder(int [] pushA,int [] popA) {
        //栈的压入弹出
        //创建一个辅助站
        Stack<Integer> stack=new Stack<>();
        int pA=0;
        int pB=0;
        while (pB<popA.length) {
            if (!stack.empty() && stack.peek() == popA[pB]) {
                pB++;
                stack.pop();
            } else {
                if(pA==pB) {
                    return false;
                }
                stack.add(pushA[pA++]);
            }
        }
        return true;
    }
    public static void main(String[] args) {
        int[]array1={1};
        int[]array2={1};

        System.out.println(IsPopOrder(array1, array2));

    }
}
